/*
 * @Descripttion: 
 * @version: 
 * @Author: jwZhang
 * @Date: 2020-04-30 20:44:32
 * @LastEditors: jwZhang
 * @LastEditTime: 2020-05-10 16:52:52
 */
import 'package:cloud_music/core/datamodels/user.dart';
import 'package:cloud_music/core/viewmodels/user_model.dart';
import 'package:cloud_music/ui/widgets/QButton.dart';
import 'package:cloud_music/ui/widgets/v_empty_view.dart';
import 'package:cloud_music/utils/navigator_util.dart';
import 'package:cloud_music/utils/utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:provider/provider.dart';

class LoginPage extends StatefulWidget {
  LoginPage({Key key}) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> with TickerProviderStateMixin {
  // 定义动画控制器
  AnimationController _controller;
  // 定义动画
  Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    // 初始化动画值
    _controller =
        AnimationController(vsync: this, duration: Duration(milliseconds: 300));
    this._animation =
        CurvedAnimation(parent: _controller, curve: Curves.linear);
    // 延时 开始动画
    Future.delayed(Duration(milliseconds: 500), () {
      _controller.forward();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        // elevation 阴影
        appBar: AppBar(
            backgroundColor: Colors.white,
            elevation: 0,
            brightness: Brightness.light),
        body: _body());
  }

  // 内容区域
  Widget _body() {
    return SingleChildScrollView(
      child: Container(
        padding: EdgeInsets.only(
            left: ScreenUtil().setWidth(80),
            right: ScreenUtil().setWidth(80),
            top: ScreenUtil().setWidth(30)),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            // logo
            Hero(
                tag: 'logo',
                child: Image.asset('assets/images/icon_logo.png',
                    width: ScreenUtil().setWidth(90),
                    height: ScreenUtil().setHeight(90))),
            // 登录表单
            _LoginFormWidget()
          ],
        ),
      ),
    );
  }
}

// 登录表单
class _LoginFormWidget extends StatefulWidget {
  _LoginFormWidget({Key key}) : super(key: key);

  @override
  __LoginFormWidgetState createState() => __LoginFormWidgetState();
}

class __LoginFormWidgetState extends State<_LoginFormWidget> {
  // 账户
  final TextEditingController _accountController = TextEditingController();
  // 密码
  final TextEditingController _pwdController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Theme(
      data: ThemeData(primaryColor: Colors.red),
      child: Column(
        // crossAxisAlignment: CrossAxisAlignment.start,
        // mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          _topTitleText(),
          // SizedBox(height: ScreenUtil().setHeight(50))
          VEmptyView(50),
          _inputFiels(),
          VEmptyView(120),
          _loginButton()
        ],
      ),
    );
  }

  // top
  Widget _topTitleText() {
    return Column(
      children: <Widget>[
        Container(
          margin: EdgeInsets.only(top: ScreenUtil().setWidth(30)),
          child: Text(
            'Welcome Back!',
            style: TextStyle(
              fontWeight: FontWeight.bold,
              color: Colors.black87,
              fontSize: 34,
            ),
          ),
        ),
        Container(
          margin: EdgeInsets.only(top: ScreenUtil().setWidth(3)),
          child: Text(
            'The Flutter Netease Cloud Music App',
            style: TextStyle(
              color: Colors.grey,
              fontSize: 14,
            ),
          ),
        )
      ],
    );
  }

  // 输入框
  Widget _inputFiels() {
    return Column(children: <Widget>[
      TextField(
        controller: _accountController,
        keyboardType: TextInputType.emailAddress,
        decoration: InputDecoration(
            hintText: '用户名/邮箱/手机号',
            prefixIcon: Icon(Icons.account_circle, color: Colors.grey)),
      ),
      VEmptyView(40),
      TextField(
        controller: _pwdController,
        keyboardType: TextInputType.visiblePassword,
        decoration: InputDecoration(
            hintText: '密码', prefixIcon: Icon(Icons.lock, color: Colors.grey)),
      ),
    ]);
  }

  // 登录按钮
  Widget _loginButton() {
    return Consumer<UserModel>(
        builder: (BuildContext context, UserModel vmodel, Widget child) {
      return QButton(
          onClick: () => _login(context, vmodel),
          title: '登录',
          width: double.infinity);
    });
  }

  // 登录
  _login(BuildContext context, UserModel vmodel) {
    String account = _accountController.text;
    String password = _pwdController.text;
    if (account.isEmpty || password.isEmpty) {
      return Utils.showToast('请输入账号或者密码');
    }
    // TODO: 没有登录
    return NavigatorUtil.goHomePage(context);
    // 登录,并存储状态
    vmodel.login(context, account, password).then((value) {
      if (value != null) {
        // 去首页
        NavigatorUtil.goHomePage(context);
      }
    });
  }
}
// class _LoginPageState extends State<LoginPage> {
//   @override
//   Widget build(BuildContext context) {
//     return WillPopScope(
//       // 重载系统的返回键
//       onWillPop: () async {
//         Navigator.pop(context, 'fromLogin');
//         return false;
//       },
//       child: Scaffold(
//         floatingActionButton: FloatingActionButton(
//           onPressed: () {
//             Navigator.pop(context, 'fromLogin');
//           },
//         ),
//         body: Center(
//           child: Text('Login'),
//         ),
//       ),
//     );
//   }
// }
